import 'dart:convert';

import 'package:hxdj/common/base_page/base_page.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'package:hxdj/common/routers/navigator.dart';
import 'package:hxdj/home/order/order_create_page.dart';
import 'package:hxdj/home/order/order_create_viewmodel.dart';
import 'package:provider/provider.dart';

class ProjectDetailsViewModel extends BaseViewModel {
  late ProjectDetailsModel detailModel;

  late String? tId, gId;
  @override
  requestData({Function(dynamic)? onSuccess, Function(dynamic)? onError, String? tmId, String? gmId}) async {
    setState(ViewState.loading);
    tId = tmId;
    gId = gmId;

    Map<String, dynamic> paramsMap = {};

    // 去请求服务器数据.
    if (tmId!.isNotEmpty && gmId!.isNotEmpty) {
      paramsMap["template_id"] = tmId;
      paramsMap["goods_id"] = gmId;
    }
    if (gmId!.isNotEmpty && tmId.isEmpty) {
      paramsMap["template_id"] = gmId;
    }

    // 去请求服务器数据.
    Map<String, dynamic> resultMap =
        await DioHttpManager().request(MyApi.getProjectInfo, method: DioHttpManager.post, data: paramsMap);
    // 请求成功.
    detailModel = ProjectDetailsModel.fromMap(resultMap["data"]);
    // 更新页面.
    setState(ViewState.normal);
  }

  //创建订单
  createOrder() {
    //创建订单,把一些相关数据, 给到关联的viewmodel页面
    OrderCreateViewModel orderCreateViewModel = Provider.of<OrderCreateViewModel>(context, listen: false);
    orderCreateViewModel.gId = "";
    orderCreateViewModel.tId = "";

    orderCreateViewModel.detailModel = detailModel;

    if (tId!.isNotEmpty && gId!.isNotEmpty) {
      orderCreateViewModel.tId = tId!;
      orderCreateViewModel.gId = gId!;
    }
    if (gId!.isNotEmpty && tId!.isEmpty) {
      orderCreateViewModel.tId = gId!;
    }

    //跳转到订单创建页面
    NavigatorUtils.pushParams(context, OrderCreatePage.routeName);
  }

  requestPayManager(String price) async {}
}

class ProjectDetailsModel {
  final int? templateId;
  final String? goodsId;
  final int? mId;
  final int? fCategoryId;
  final int? sCategoryId;
  final String? price;
  final String? title;
  final String? coverImg;
  final int? serviceDuration;
  final String? desc;
  final List<DescHighlight>? descHighlight;
  final String? descImg;
  final List<String>? tags;
  final List<GoodsImg>? goodsImgs;
  final List<GoodsImg>? goodsDescImgs;
  final int? logisticsId;
  final String? logisticsDesc;
  final String? serviceDurationStr;
  final int? sales;
  final String? salesStr;
  final int? jumpType;
  final List<UserNeedKnow>? userNeedKnow;
  final String? couponInfoStr;
  final String? goodsRemindTitle;
  final String? goodsRemindText;
  final String? goodsServiceFeeStr;
  final int? isDiscounts;
  final double? discountsRate;
  final String? goodsDiscountPrice;
  final String? goodsHighDiscountPrice;
  final int? isVip;
  final String? purchaseInstructionsTip;
  final String? anmoRoadMoneyTip;

  ProjectDetailsModel({
    this.templateId,
    this.goodsId,
    this.mId,
    this.fCategoryId,
    this.sCategoryId,
    this.price,
    this.title,
    this.coverImg,
    this.serviceDuration,
    this.desc,
    this.descHighlight,
    this.descImg,
    this.tags,
    this.goodsImgs,
    this.goodsDescImgs,
    this.logisticsId,
    this.logisticsDesc,
    this.serviceDurationStr,
    this.sales,
    this.salesStr,
    this.jumpType,
    this.userNeedKnow,
    this.couponInfoStr,
    this.goodsRemindTitle,
    this.goodsRemindText,
    this.goodsServiceFeeStr,
    this.isDiscounts,
    this.discountsRate,
    this.goodsDiscountPrice,
    this.goodsHighDiscountPrice,
    this.isVip,
    this.purchaseInstructionsTip,
    this.anmoRoadMoneyTip,
  });

  factory ProjectDetailsModel.fromJson(String str) => ProjectDetailsModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory ProjectDetailsModel.fromMap(Map<String, dynamic> json) => ProjectDetailsModel(
        templateId: json["template_id"],
        goodsId: json["goods_id"],
        mId: json["m_id"],
        fCategoryId: json["f_category_id"],
        sCategoryId: json["s_category_id"],
        price: json["price"],
        title: json["title"],
        coverImg: json["cover_img"],
        serviceDuration: json["service_duration"],
        desc: json["desc"],
        descHighlight: json["desc_highlight"] == null
            ? []
            : List<DescHighlight>.from(json["desc_highlight"]!.map((x) => DescHighlight.fromMap(x))),
        descImg: json["desc_img"],
        tags: json["tags"] == null ? [] : List<String>.from(json["tags"]!.map((x) => x)),
        goodsImgs:
            json["goods_imgs"] == null ? [] : List<GoodsImg>.from(json["goods_imgs"]!.map((x) => GoodsImg.fromMap(x))),
        goodsDescImgs: json["goods_desc_imgs"] == null
            ? []
            : List<GoodsImg>.from(json["goods_desc_imgs"]!.map((x) => GoodsImg.fromMap(x))),
        logisticsId: json["logistics_id"],
        logisticsDesc: json["logistics_desc"],
        serviceDurationStr: json["service_duration_str"],
        sales: json["sales"],
        salesStr: json["sales_str"],
        jumpType: json["jump_type"],
        userNeedKnow: json["user_need_know"] == null
            ? []
            : List<UserNeedKnow>.from(json["user_need_know"]!.map((x) => UserNeedKnow.fromMap(x))),
        couponInfoStr: json["coupon_info_str"],
        goodsRemindTitle: json["goods_remind_title"],
        goodsRemindText: json["goods_remind_text"],
        goodsServiceFeeStr: json["goods_service_fee_str"],
        isDiscounts: json["isDiscounts"],
        discountsRate: json["discountsRate"]?.toDouble(),
        goodsDiscountPrice: json["goodsDiscountPrice"],
        goodsHighDiscountPrice: json["goodsHighDiscountPrice"],
        isVip: json["isVip"],
        purchaseInstructionsTip: json["purchase_instructions_tip"],
        anmoRoadMoneyTip: json["anmo_road_money_tip"],
      );

  Map<String, dynamic> toMap() => {
        "template_id": templateId,
        "goods_id": goodsId,
        "m_id": mId,
        "f_category_id": fCategoryId,
        "s_category_id": sCategoryId,
        "price": price,
        "title": title,
        "cover_img": coverImg,
        "service_duration": serviceDuration,
        "desc": desc,
        "desc_highlight": descHighlight == null ? [] : List<dynamic>.from(descHighlight!.map((x) => x.toMap())),
        "desc_img": descImg,
        "tags": tags == null ? [] : List<dynamic>.from(tags!.map((x) => x)),
        "goods_imgs": goodsImgs == null ? [] : List<dynamic>.from(goodsImgs!.map((x) => x.toMap())),
        "goods_desc_imgs": goodsDescImgs == null ? [] : List<dynamic>.from(goodsDescImgs!.map((x) => x.toMap())),
        "logistics_id": logisticsId,
        "logistics_desc": logisticsDesc,
        "service_duration_str": serviceDurationStr,
        "sales": sales,
        "sales_str": salesStr,
        "jump_type": jumpType,
        "user_need_know": userNeedKnow == null ? [] : List<dynamic>.from(userNeedKnow!.map((x) => x.toMap())),
        "coupon_info_str": couponInfoStr,
        "goods_remind_title": goodsRemindTitle,
        "goods_remind_text": goodsRemindText,
        "goods_service_fee_str": goodsServiceFeeStr,
        "isDiscounts": isDiscounts,
        "discountsRate": discountsRate,
        "goodsDiscountPrice": goodsDiscountPrice,
        "goodsHighDiscountPrice": goodsHighDiscountPrice,
        "isVip": isVip,
        "purchase_instructions_tip": purchaseInstructionsTip,
        "anmo_road_money_tip": anmoRoadMoneyTip,
      };
}

class DescHighlight {
  final String? title;
  final String? color;
  final String? fontSize;

  DescHighlight({
    this.title,
    this.color,
    this.fontSize,
  });

  factory DescHighlight.fromJson(String str) => DescHighlight.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory DescHighlight.fromMap(Map<String, dynamic> json) => DescHighlight(
        title: json["title"],
        color: json["color"],
        fontSize: json["font_size"],
      );

  Map<String, dynamic> toMap() => {
        "title": title,
        "color": color,
        "font_size": fontSize,
      };
}

class GoodsImg {
  final int? goodsImgId;
  final String? img;
  final int? sort;

  GoodsImg({
    this.goodsImgId,
    this.img,
    this.sort,
  });

  factory GoodsImg.fromJson(String str) => GoodsImg.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory GoodsImg.fromMap(Map<String, dynamic> json) => GoodsImg(
        goodsImgId: json["goods_img_id"],
        img: json["img"],
        sort: json["sort"],
      );

  Map<String, dynamic> toMap() => {
        "goods_img_id": goodsImgId,
        "img": img,
        "sort": sort,
      };
}

class UserNeedKnow {
  final String? content;
  final String? keyword;
  final String? title;
  final String? url;

  UserNeedKnow({
    this.content,
    this.keyword,
    this.title,
    this.url,
  });

  factory UserNeedKnow.fromJson(String str) => UserNeedKnow.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory UserNeedKnow.fromMap(Map<String, dynamic> json) => UserNeedKnow(
        content: json["content"],
        keyword: json["keyword"],
        title: json["title"],
        url: json["url"],
      );

  Map<String, dynamic> toMap() => {
        "content": content,
        "keyword": keyword,
        "title": title,
        "url": url,
      };
}
